package com.ag.improvedjava.utils;



public class Requires {

	public static <T> T requireNonNull(T t) {
		if (t == null) {
			throw new IllegalArgumentException();
		}
		return t;	
	}
	
	public static <T> T requireNonNull(T t, String message, Object...args) {
		if (t == null) {
			String formattedMessage = String.format(String.valueOf(message), args);
			throw new IllegalArgumentException(formattedMessage);
		}
		return t;
	}
	
	public static <T> T requireCondition(T t, Predicate<T> predicate) {
		requireNonNull(predicate);
		if (!predicate.evaluate(t)) {
			throw new IllegalArgumentException();
		}
		return t;
	}
	
	public static <T> T requireCondition(T t, Predicate<T> predicate, String message, Object...args) {
		requireNonNull(predicate, "The predicate can not be null.");
		if (!predicate.evaluate(t)) {
			String formattedMessage = String.format(String.valueOf(message), args);
			throw new IllegalArgumentException(formattedMessage);
		}
		return t;
	}
	
	public static <T> T requireNonNullAndCondition(T t, Predicate<T> predicate) {
		return requireCondition(requireNonNull(t), predicate);
	}

	public static <T> T requireNonNullAndCondition(T t, Predicate<T> predicate, String message, Object...args) {
		String formattedMessage = String.format(String.valueOf(message), args);
		return requireCondition(requireNonNull(t, message, args), predicate, formattedMessage);
	}

	
}
